/*
 * EF_BLOG_Record.java
 *
 * Created on 6. November 2006, 14:09
 *
 *  This file is part of JSmex.
 *
 *  JSmex is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  JSmex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package jsmex.function;

/**
 *
 * @author Tobias Senger
 */
public class EF_BLOG_Record {
    
    private byte status;
    private byte[] bseq = new byte[2];
    private byte[] hkn = new byte[10]; // Händlerkartennummer 
    private byte[] hseq = new byte[4];
    private byte[] betrag = new byte[3];
    private byte[] restbetrag = new byte[3];
    private byte[] datum = new byte[4];
    private byte[] uhrzeit = new byte[3];
    private byte kid;
    
    
    /**
     * Creates a new instance of EF_BLOG_Record
     */
    public EF_BLOG_Record(byte[] data) { 
        status = data[0];
        copyByteArray(data, bseq, 1,2);
        copyByteArray(data, hkn, 3,10);
        copyByteArray(data, hseq, 13,4);
        copyByteArray(data, betrag, 20,3);
        copyByteArray(data, restbetrag, 23,3);
        copyByteArray(data, datum, 28,4);
        copyByteArray(data, uhrzeit, 32,3);
        kid = data[35];
    }
    
    private void copyByteArray(byte[] from, byte[] to, int fromstart, int length) {
        for (int i= 0;i<length;i++) to[i] = from[i+fromstart];
    }
    
    private int toUnsignedInt(byte value) {
	return (value & 0x7F) + (value < 0 ? 128 : 0);
    }
    
    private long hexToLong(byte[] ba) {
        long value = 0;
        for (int i=0;i<ba.length;i++) {
            value = value + (toUnsignedInt(ba[ba.length-1-i])*(long)Math.pow((double)256,(double)i));
        }
        return value;
    }
    
    private String formatBetragString(String betrag) {
        StringBuffer sb = new StringBuffer(betrag);
        sb.insert(4,",");
        while (sb.charAt(0)=='0') { 
            sb.deleteCharAt(0);
        }
        if (sb.charAt(0)==',') sb.insert(0,'0');
        return sb.toString();
    }
    
    private String BCDByteArrayToString(byte[] ba, int start, int end) {
         StringBuffer sb = new StringBuffer();
         byte b1;
         for (int i = start; i<end; i++) {
            b1 = ba[i];
            sb.append(toUnsignedInt(b1)>>>4);
            sb.append(toUnsignedInt(ba[i])&0x0F);
         }
         return sb.toString();
    }
    
    public int getStatus() {
        return toUnsignedInt(status);
    }
    
    public long getBSEQ() {
        return hexToLong(bseq);
    }
        
    public String getBetrag() {
        return formatBetragString( BCDByteArrayToString(betrag,0,betrag.length) );
    }
    
    public String getHKN() {
        return BCDByteArrayToString(hkn,0,hkn.length);
    }
    
    public long getHSEQ() {
        return hexToLong(hseq);
    }
    
    public String getRestBetrag() {
        return formatBetragString( BCDByteArrayToString(restbetrag,0,restbetrag.length) );
    }
    
    public String getDatum() {
        StringBuffer sb = new StringBuffer( BCDByteArrayToString(datum,0,datum.length) );
        String jahr = sb.substring(0,4); 
        String monat = sb.substring(4,6);
        String tag = sb.substring(6,8);
        return tag+"."+monat+"."+jahr;
    }
    
    public String getUhrzeit() {
        StringBuffer sb = new StringBuffer( BCDByteArrayToString(uhrzeit,0,uhrzeit.length) );
        String stunde = sb.substring(0,2);
        String minute = sb.substring(2,4);
        String sekunde = sb.substring(4,6);
        return stunde+":"+minute+":"+sekunde;
    }
    
    public int getKID() {
        return toUnsignedInt(kid);
    }
}
